﻿using System;
using System.Linq;
using BookingSystem.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BookingSystem.Mappings
{
    [TestClass]
    public class GuestMapTests
    {
        [TestInitialize]
        public void TestInitialize()
        {
            //Create 10 Rooms with 3 beds in each
            using (var session = BookingSystem.Config.CreateSessionFactory().OpenSession())
            {
                for (int i = 0; i < 9; i++)
                {
                    var resource = new Resource();
                    resource.Description = string.Format("Rum {0} {1}",i,"Säng A");
                    resource.Description = string.Format("Rum {0} {1}", i, "Säng B");
                    resource.Description = string.Format("Rum {0} {1}", i, "Säng C");
                    session.SaveOrUpdate(resource);
                    session.Flush();
                }
            }

        }

        [TestMethod]
        public void SaveGuest_WithoutBookings_SavedWithId1()
        {
            using (var session = BookingSystem.Config.CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var entityName = new Guest();
                    entityName.FirstName = "Johan";
                    entityName.LastName = "Spännare";
                    entityName.Address = "Anders Peters väg 5";
                    entityName.Postalcode = "702 21";
                    entityName.City = "Örebro";
                    entityName.Member = true;
                    entityName.Email = "js@seb.se";

                    session.SaveOrUpdate(entityName);

                    transaction.Commit();
                }
                var guest = session.CreateCriteria(typeof (Guest)).List<Guest>();

                Assert.AreEqual(guest.First().Id, 1);
            }
        }

        [TestMethod]
        public void SaveGuest_WithBooking_SavedWithId1()
        {
            using (var session = BookingSystem.Config.CreateSessionFactory().OpenSession())
            {

                using (var transaction = session.BeginTransaction())
                {
                    var guest = new Guest();
                    guest.FirstName = "Johan";
                    guest.LastName = "Spännare";
                    guest.Address = "Anders Peters väg 5";
                    guest.Postalcode = "702 21";
                    guest.City = "Örebro";
                    guest.Member = true;
                    guest.Email = "js@seb.se";
                    
                    var bed = new Resource();
                    
                    var booking = new Booking();
                    booking.Resource = bed;
                    booking.StartDate = DateTime.Today;
                    booking.EndDate = DateTime.Today.AddDays(10);
                    booking.Guest = guest;

                    guest.Bookings.Add(booking);

                    session.SaveOrUpdate(bed);
                    session.SaveOrUpdate(booking);
                    session.SaveOrUpdate(guest);

                    transaction.Commit();
                }
                var guests = session.CreateCriteria(typeof (Guest)).List<Guest>();

                Assert.AreEqual(guests.First().Bookings.First().Id, 1);
                Assert.AreEqual(guests.First().Bookings.First().Guest, guests.First());
            }
        }
    }
}